﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; 
using System.Configuration;
using LapvWebComponent;
using LapvBussiness;

namespace LapvWebM
{
    public class SessionLogin
    {
        public static int GetUsserId()
        {
            int userid = 0;
            if (HttpContext.Current.User.Identity.IsAuthenticated == false) return 0;
            if (HttpContext.Current.Session["UserId"] == null)
            {
                string UserName = HttpContext.Current.User.Identity.Name;
                account acc = null;
                if (!string.IsNullOrEmpty(UserName))
                {
                    using (lapvwebEntities db = new lapvwebEntities())
                    {
                        acc = db.accounts.Include("account_profile").Where(en => en.username == UserName).FirstOrDefault();
                    }
                }
                if (acc != null)
                {
                    userid = acc.userid;

                    HttpContext.Current.Session["UserId"] = acc.userid;
                    HttpContext.Current.Session["UserName"] = acc.username;
                    //HttpContext.Current.Session["FullName"] = acc.fullname;
                    HttpContext.Current.Session["Password"] = acc.password;
                    HttpContext.Current.Session["ClassId"] = acc.account_profile.ClassId;
                    HttpContext.Current.Session["Cash"] = acc.cash;
                    if (acc.account_profile != null)
                    {
                        if (!string.IsNullOrEmpty(acc.account_profile.avatar))
                            HttpContext.Current.Session["Avatar"] = acc.account_profile.avatar;
                        else HttpContext.Current.Session["Avatar"] = "ico_avatar.png";
                    }
                    else HttpContext.Current.Session["Avatar"] = "ico_avatar.png";
                    //if (!string.IsNullOrEmpty(acc.passgame))
                    //    HttpContext.Current.Session["PassGame"] = acc.passgame;
                    //if (!string.IsNullOrEmpty(acc.gamesetpass))
                    //    HttpContext.Current.Session["ListGamePass"] = acc.gamesetpass;
                }
            }
            else
            {
                int.TryParse(HttpContext.Current.Session["UserId"].ToString(), out userid);
            }
            return userid;
        }
        public static int GetUsserId(HttpContext context)
        {
            int userid = 0;
            if (context.Session["UserId"] == null)
            {
                string UserName = context.User.Identity.Name;
                account acc = Provider.GetUserByUserName(UserName);
                if (acc != null)
                {
                    userid = acc.userid;
                    context.Session["UserId"] = acc.userid;
                    context.Session["UserName"] = acc.username;
                //    context.Session["FullName"] = acc.fullname;
                    context.Session["Password"] = acc.password;
                    context.Session["ClassId"] = acc.account_profile.ClassId;
                    if (acc.password == null)
                    //    context.Session["ListGame"] = "";
                    //else
                    //    context.Session["ListGame"] = acc.loggame;
                    //context.Session["Cash"] = acc.cash;
                    if (acc.account_profile != null)
                    {
                        if (!string.IsNullOrEmpty(acc.account_profile.avatar))
                            context.Session["Avatar"] = acc.account_profile.avatar;
                        else
                            context.Session["Avatar"] = "ico_avatar.png";
                    }
                    else
                        context.Session["Avatar"] = "ico_avatar.png";
                    //Get count private_message isread==false
                    //Luu vao session
                    //if (acc.userid > 0)
                    //{
                    //    context.Session["countMessage"] = Provider.GetCountMessageNotRead(acc.userid);
                    //}
                }
            }
            else
            {
                int.TryParse(context.Session["UserId"].ToString(), out userid);
            }
            return userid;
        }
    }
}
